package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.system.domain.MineQuestion;
import com.ruoyi.system.mapper.MineExamMapper;
import com.ruoyi.system.mapper.MineQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MineExamQuestionMapper;
import com.ruoyi.system.domain.MineExamQuestion;
import com.ruoyi.system.service.IMineExamQuestionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 试卷题目关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@Service
public class MineExamQuestionServiceImpl implements IMineExamQuestionService 
{
    @Autowired
    private MineExamQuestionMapper mineExamQuestionMapper;

    @Autowired
    private MineQuestionMapper mineQuestionMapper;

    @Autowired
    private MineExamMapper mineExamMapper;

    /**
     * 查询试卷题目关联
     * 
     * @param examId 试卷题目关联主键
     * @return 试卷题目关联
     */
    @Override
    public MineExamQuestion selectMineExamQuestionByExamId(Long examId)
    {
        return mineExamQuestionMapper.selectMineExamQuestionByExamId(examId);
    }

    /**
     * 查询试卷题目关联列表
     * 
     * @param mineExamQuestion 试卷题目关联
     * @return 试卷题目关联
     */
    @Override
    public List<MineExamQuestion> selectMineExamQuestionList(MineExamQuestion mineExamQuestion)
    {
        return mineExamQuestionMapper.selectMineExamQuestionList(mineExamQuestion);
    }

    /**
     * 新增试卷题目关联
     * 
     * @param mineExamQuestion 试卷题目关联
     * @return 结果
     */
    @Override
    public int insertMineExamQuestion(MineExamQuestion mineExamQuestion)
    {
        return mineExamQuestionMapper.insertMineExamQuestion(mineExamQuestion);
    }

    @Override
    @Transactional  // 确保删除旧数据和插入新数据在同一个事务中
    public int insertBatchMineExamQuestion(List<MineExamQuestion> list) {
        return mineExamQuestionMapper.insertBatchMineExamQuestion(list);
    }
    /**
     * 修改试卷题目关联
     * 
     * @param mineExamQuestion 试卷题目关联
     * @return 结果
     */
    @Override
    public int updateMineExamQuestion(MineExamQuestion mineExamQuestion)
    {
        return mineExamQuestionMapper.updateMineExamQuestion(mineExamQuestion);
    }

    /**
     * 批量删除试卷题目关联
     * 
     * @param examIds 需要删除的试卷题目关联主键
     * @return 结果
     */
    @Override
    public int deleteMineExamQuestionByExamIds(Long[] examIds)
    {
        return mineExamQuestionMapper.deleteMineExamQuestionByExamIds(examIds);
    }

    /**
     * 删除试卷题目关联信息
     * 
     * @param examId 试卷题目关联主键
     * @return 结果
     */
    @Override
    public int deleteMineExamQuestionByExamId(Long examId)
    {
        return mineExamQuestionMapper.deleteMineExamQuestionByExamId(examId);
    }

    @Override
    public List<MineQuestion> selectMineQuestionsByExamId(Long examId) {
        // 1. 查询题目ID列表
        List<Long> questionIds = mineExamQuestionMapper.selectMineQuestionIdsByExamId(examId);
        if (questionIds.isEmpty()) {
            return Collections.emptyList();
        }
        // 2. 根据题目ID列表查询题目详情
        return mineQuestionMapper.selectMineQuestionByIds(questionIds);
    }
}
